﻿using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using DevExpress.XtraGrid.Views.Grid;

namespace QLTK
{
    public partial class frmDetail : Form
    {
        public frmDetail(DataRow tokhai)
        {
            InitializeComponent();
            showDetail(tokhai);
            showHangAndMuon(tokhai);
        }

        private void showDetail(DataRow tokhai)
        {
            labelSotk.Text = tokhai["SoTK"].ToString();
            labelMalh.Text = tokhai["MaLH"].ToString();
            labelMahq.Text = tokhai["MaHQ"].ToString();

            // convert date cho ngày đăng ký
            DateTime ngaydk = DateTime.Parse(tokhai["NgayDK"].ToString());
            labelNgaydk.Text = ngaydk.ToString("dd/MM/yyyy");

            // convert date cho ngày lưu và kiểm tra rỗng
            if (tokhai["NgayLuu"].ToString() != "")
            {
                DateTime ngayluu = DateTime.Parse(tokhai["NgayLuu"].ToString());
                labelNgayluu.Text = ngayluu.ToString("dd/MM/yyyy");
            }
            else
            {
                labelNgayluu.Text = "";
            }

            labelVitri.Text = tokhai["ViTri"].ToString();
            labelMadv.Text = tokhai["MaDV"].ToString();

            // set màu cho label trạng thái
            labelTrangthai.Text = tokhai["TrangThai"].ToString();
            if (tokhai["TrangThai"].ToString() == "Đã sắp xếp")
            {
                labelTrangthai.BackColor = Color.PowderBlue;
            }
            else if (tokhai["TrangThai"].ToString() == "Đang cho mượn")
            {
                labelTrangthai.BackColor = Color.PeachPuff;
            }
            else if (tokhai["TrangThai"].ToString() == "Chưa bàn giao")
            {
                labelTrangthai.BackColor = Color.Empty;
            }
            else
            {
                labelTrangthai.BackColor = Color.Gainsboro;
            }

            labelTendv.Text = tokhai["TenDV"].ToString();
        }

        private void showHangAndMuon(DataRow tokhai)
        {
            int sotk = Convert.ToInt32(tokhai["SoTK"].ToString());
            string malh = tokhai["MaLH"].ToString();
            string mahq = tokhai["MaHQ"].ToString();
            short namdk = Convert.ToInt16(tokhai["NamDK"].ToString());

            try
            {
                hangTableAdapter.HangByToKhai(qLTKDataSet.Hang, sotk, malh, mahq, namdk);
                muonToKhaiTableAdapter.MuonByToKhai(qLTKDataSet.MuonToKhai, sotk, malh, mahq, namdk);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void frmDetail_Load(object sender, EventArgs e)
        {
        }

        private void gridViewMuon_RowStyle(object sender, RowStyleEventArgs e)
        {
            var view = sender as GridView;
            if (e.RowHandle >= 0)
            {
                string status = view.GetRowCellDisplayText(e.RowHandle, view.Columns["TrangThai"]);
                if (status == "Đã trả")
                {
                    e.Appearance.BackColor = Color.PowderBlue;
                    e.Appearance.BackColor2 = Color.PowderBlue;
                }
                else if (status == "Chưa trả")
                {
                    e.Appearance.BackColor = Color.PeachPuff;
                    e.Appearance.BackColor2 = Color.PeachPuff;
                }
                else
                {
                    e.Appearance.BackColor = Color.Gainsboro;
                    e.Appearance.BackColor2 = Color.Gainsboro;
                }
            }
        }
    }
}